public class FindFirstAndLastPositionOfElementInSortedArray {
    public int[] searchRange(int[] nums, int target) {
        int n = nums.length;
        int[] ret = {-1, -1};
        if(n == 0) {
            return ret;
        }

        int left = 0, right = n - 1;
        // 找左端点
        while(left < right) {
            int mid = left + ((right - left) >> 1);
            if(nums[mid] < target) {
                left = mid + 1;
            }else {
                right = mid;
            }
        }

        if(nums[left] != target) {
            return ret;
        }
        ret[0] = left;

        // 找右端点, 记得 right 指针归位
        right = n - 1;
        while(left < right) {
            int mid = left + ((right - left + 1) >> 1);
            if(nums[mid] <= target) {
                left = mid;
            }else {
                right = mid - 1;
            }
        }
        ret[1] = right;
        return ret;
    }
}
